package com.com.corejavalearnings.database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;

/**
 * Created by pankaj on 7/14/14.
 */
public class DataPersister implements Runnable {
    static Logger logger = Logger.getLogger(DataPersister.class.getName());

    static {
        logger.setParent(Logger.getLogger(DataPersister.class.getPackage().getName()));
    }

    private String dataSource;
    public DataPersister(String dataSource){
        this.dataSource = dataSource ;
        //logger.info("dataSource: "+ dataSource);
    }

    @Override
    public void run() {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            connection = LocalDB.getInstance(dataSource).getBoneCPConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery("select * from employee");
            while (resultSet.next()) {
                //Thread.sleep(1000);
                logger.info("employeeid: " + resultSet.getString("id"));
                logger.info("employeename: " + resultSet.getString("firstname"));
            }
        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            if (resultSet != null) try { resultSet.close(); } catch (SQLException e) {e.printStackTrace();}
            if (statement != null) try { statement.close(); } catch (SQLException e) {e.printStackTrace();}
            if (connection != null) try { connection.close(); } catch (SQLException e) {e.printStackTrace();}
        }
    }
}
